package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;

import java.util.List;

/**
 * 禁用时间段列表Activity
 * 显示所有禁用时间段
 */
public class BlockTimeListActivity extends AppCompatActivity {

    private BlockTimeRepository blockTimeRepository;
    private LinearLayout containerBlockTimes;
    private TextView btnAddBlockTime;
    private ImageView btnBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_block_time_list);

        blockTimeRepository = new BlockTimeRepository(this);
        
        initViews();
        setupListeners();
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadBlockTimes();
    }

    private void initViews() {
        containerBlockTimes = findViewById(R.id.container_block_times);
        btnAddBlockTime = findViewById(R.id.btn_add_block_time);
        btnBack = findViewById(R.id.btn_back);
    }

    private void setupListeners() {
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        btnAddBlockTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BlockTimeListActivity.this, AddBlockTimeActivity.class);
                startActivity(intent);
            }
        });
    }

    private void loadBlockTimes() {
        containerBlockTimes.removeAllViews();
        
        List<BlockTimeSlot> blockTimes = blockTimeRepository.getAllBlockTimes();
        
        for (final BlockTimeSlot slot : blockTimes) {
            View itemView = LayoutInflater.from(this).inflate(R.layout.item_block_time, containerBlockTimes, false);
            
            TextView textTimeRange = itemView.findViewById(R.id.text_time_range);
            TextView textRepeat = itemView.findViewById(R.id.text_repeat);
            SwitchCompat switchEnabled = itemView.findViewById(R.id.switch_enabled);
            
            textTimeRange.setText(slot.getTimeRangeText());
            textRepeat.setText(slot.getRepeatText());
            switchEnabled.setChecked(slot.isEnabled());
            
            // 开关监听
            switchEnabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    slot.setEnabled(isChecked);
                    blockTimeRepository.updateBlockTime(slot);
                }
            });
            
            // 点击编辑
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(BlockTimeListActivity.this, AddBlockTimeActivity.class);
                    intent.putExtra(AddBlockTimeActivity.EXTRA_BLOCK_TIME_ID, slot.getId());
                    startActivity(intent);
                }
            });
            
            // 长按删除
            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    showDeleteConfirmDialog(slot);
                    return true;
                }
            });
            
            containerBlockTimes.addView(itemView);
        }
    }

    private void showDeleteConfirmDialog(final BlockTimeSlot slot) {
        new android.app.AlertDialog.Builder(this)
            .setTitle("删除时间段")
            .setMessage("确定要删除这个时间段吗？")
            .setPositiveButton("删除", new android.content.DialogInterface.OnClickListener() {
                @Override
                public void onClick(android.content.DialogInterface dialog, int which) {
                    blockTimeRepository.deleteBlockTime(slot.getId());
                    loadBlockTimes();
                    Toast.makeText(BlockTimeListActivity.this, "已删除", Toast.LENGTH_SHORT).show();
                }
            })
            .setNegativeButton("取消", null)
            .show();
    }
}

